<template>
    <div class="app-container">
        <!-- 医院设置添加 -->
        <el-form label-width="120px">
          <el-form-item label="医院名称">
              <el-input v-model="hospSet.hosname"/>
          </el-form-item>
          <el-form-item label="医院编号">
              <el-input v-model="hospSet.hoscode"/>
          </el-form-item>
          <el-form-item label="api基础路径">
              <el-input v-model="hospSet.apiUrl"/>
          </el-form-item>
          <el-form-item label="联系人姓名">
              <el-input v-model="hospSet.contactsName"/>
          </el-form-item>
          <el-form-item label="联系人手机">
              <el-input v-model="hospSet.contactsPhone"/>
          </el-form-item>

          <el-form-item label="">
              <el-button type="primary" @click="saveOrUpdate">保存</el-button>
          </el-form-item>
        </el-form>
    </div>
</template>
<script>
import hospitalset from '@/api/hospitalset'

export default {
    data() {
        return {
            hospSet: {

            }
        }
    },
    created() {//页面渲染之前执行
        if(this.$route.params && this.$route.params.id) {
            //获取路由中的id
            const id = this.$route.params.id
            //根据id查询信息
            this.getHospSetById(id)
        } else {
            //清空表单数据
            this.hospSet = {}
        }
    },
    methods: {
        //添加
        save() {
            //此时是新增
            hospitalset.saveHospSet(this.hospSet)
            .then((response) => {
                //提示信息
                this.$message({
                    type: 'success',
                    message: '添加成功！'
                })
                //跳转到列表，使用路由跳转
                this.$router.push({path: '/hospital/list'})
            })
        },
        //修改
        update() {
            //此时是修改
            hospitalset.updateHospSet(this.hospSet)
            .then((response) => {
                //提示信息
                this.$message({
                    type: 'success',
                    message: '修改成功！'
                })
                //跳转到列表，使用路由跳转
                this.$router.push({path: '/hospital/list'})
            })
        },
        //添加方法
        saveOrUpdate() {
            if(!this.hospSet.id) {
                this.save()
            } else {
                this.update()
            }
        },
        //根据id查询数据
        getHospSetById(id) {
            hospitalset.getHospSetById(id)
            .then((response) => {
                this.hospSet = response.data
            })
        }
    }
}
</script>
